package com.ymttest.common.order.userquery.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.util.CollectionUtils;

import com.ymatou.iapi.trading.ordersync.parameter.po.SubProduct;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.SubProductInfo;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.common.enums.PlatformEnum;
import com.ymttest.common.enums.SalesRefundStatusEnum;
import com.ymttest.database.model.mongo.query.Fund;
import com.ymttest.database.model.mongo.query.Order;
import com.ymttest.database.model.mongo.query.Refund;
import com.ymttest.utils.DataUtil;

/**
 * Created by zhangzhiqiang on 2016/12/12.
 */
public class OrderHelper {

	//获取所有订单状态
	public static List<Integer> getAllOrderStatus() {
		List<Integer> list = new ArrayList<Integer>();
		list.add(OrderStatusEnum.OrderEstablish.getValue());
		list.add(OrderStatusEnum.AccountPaid.getValue());
		list.add(OrderStatusEnum.Shipped.getValue());
		list.add(OrderStatusEnum.Received.getValue());
		list.add(OrderStatusEnum.ApplyForPostPay.getValue());
		list.add(OrderStatusEnum.SellerAccept.getValue());
		list.add(OrderStatusEnum.UserAcceptCancel.getValue());
		list.add(OrderStatusEnum.SellerAcceptCancel.getValue());
		list.add(OrderStatusEnum.SystemCancel.getValue());
		return list;
	}

	//获取已支付未取消订单状态
	public static List<Integer> getPaidOrderStatus() {
		List<Integer> list = new ArrayList<Integer>();
		list.add(OrderStatusEnum.AccountPaid.getValue());
		list.add(OrderStatusEnum.Shipped.getValue());
		list.add(OrderStatusEnum.Received.getValue());
		list.add(OrderStatusEnum.ApplyForPostPay.getValue());
		list.add(OrderStatusEnum.SellerAccept.getValue());
		return list;
	}

	//获取已完结订单状态（包括：已确认收货、已取消）
	public static List<Integer> getCompletedOrderStatus() {
		List<Integer> list = new ArrayList<Integer>();
		list.add(OrderStatusEnum.Received.getValue());
		list.add(OrderStatusEnum.UserAcceptCancel.getValue());
		list.add(OrderStatusEnum.SellerAcceptCancel.getValue());
		list.add(OrderStatusEnum.SystemCancel.getValue());
		return list;
	}

	public static PlatformEnum getPlatformEnumInfo(String orderSource) {
		String source = orderSource == null ? "" : orderSource.toUpperCase();
		switch (source) {
			case "APP" :
				return PlatformEnum.Ymt_APP;
			case "PC" :
				return PlatformEnum.Ymt_PC;
			case "WAP" :
				return PlatformEnum.Ymt_WAP;
			case "C2CAPP" :
				return PlatformEnum.Ymt_C2CAPP;
			case "C2CWAP" :
				return PlatformEnum.Ymt_C2CWAP;
			case "C2CWechat" :
				return PlatformEnum.Ymt_C2CWechat;
			case "Wechat" :
				return PlatformEnum.Ymt_Wechat;
			default :
				return PlatformEnum.Ymt_Other;
		}
	}

	public static BigDecimal getPrePaidAmount(Order order) {
		if (order == null || order.getFund() == null
				|| order.getPaidTime() == null) {
			return BigDecimal.ZERO;
		}
		Fund fund = order.getFund();
		return DataUtil.BigDecimall
				.GetValueOrDefault(fund.getPaidCash())
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund.getPaidGif()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getPaidFreeCard()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund.getYmtCoupon()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getSellerCoupon()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getDiscount3P()));
	}

	public static BigDecimal getTotalPayAmount(Order order) {
		if (order == null || order.getFund() == null) {
			return BigDecimal.ZERO;
		}
		return DataUtil.BigDecimall.GetValueOrDefault(getPrePaidAmount(order))
				.add(DataUtil.BigDecimall
						.GetValueOrDefault(getPostPaidAmount(order.getFund())));
	}

	public static BigDecimal getPostPaidAmount(Fund fund) {
		if (fund == null) {
			return BigDecimal.ZERO;
		}
		return DataUtil.BigDecimall.GetValueOrDefault(fund.getPostCash()).add(
				DataUtil.BigDecimall.GetValueOrDefault(fund.getPostGif()));
		//.add( DataUtil.BigDecimall.GetValueOrDefault(fund.getPostPadiAmountOfCoupon())); //补款没有优惠券
	}

	public static BigDecimal getAmountOfRealPay(Order order) {
		Fund fund = order.getFund();
		if (fund == null) {
			return BigDecimal.ZERO;
		}
		return getPrePaidAmount(order).add(getPostPaidAmount(fund))
				.subtract(
						DataUtil.BigDecimall
								.GetValueOrDefault(getRefundedAmount(fund)));
	}

	public static BigDecimal getCashAmountOfRealPay(Fund fund) {
		if (fund == null) {
			return BigDecimal.ZERO;
		}
		return DataUtil.BigDecimall
				.GetValueOrDefault(fund.getPaidCash())
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund.getPostCash()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(fund
								.getRefundCash()));
	}

	public static BigDecimal getGiftAmountOfRealPay(Fund fund) {
		if (fund == null) {
			return BigDecimal.ZERO;
		}
		return DataUtil.BigDecimall
				.GetValueOrDefault(fund.getPaidGif())
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund.getPostGif()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(fund
								.getRefundGif()));
	}

	/**
	 * 订单付款（预付款）时，应付的现金金额
	 *
	 * @param order
	 * @return
	 */
	public static BigDecimal getNeedPrePayAmountOfCash(Order order) {

		//订单应付金额 = 金额(M全款C定金,补款不再调整) + 调整金额 + 运费 - 红包抵扣金额 - 优惠券金额 - 商家优惠
		BigDecimal amount = DataUtil.BigDecimall
				.GetValueOrDefault(order.getOrderPrice())
				.add(DataUtil.BigDecimall.GetValueOrDefault(order
						.getPreDiscount()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(order
						.getPostDiscount()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(order.getFreight()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(
								order.getYmtCouponAmount()).add(
								DataUtil.BigDecimall.GetValueOrDefault(order
										.getSellerCouponAmount())))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(order.getFund()
								.getPaidGif()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(order
								.getSellerPromotionAmount()));

		return amount;
	}

	//获取订单中支付的现金金额
	public static BigDecimal getAmountOfCash(Fund fund) {
		return fund.getPaidCash().add(fund.getPostCash())
				.subtract(fund.getRefundCash());
	}

	//获取订单当前金额
	public static BigDecimal getOrderCurrentAmount(Fund fund) {
		return getAmountOfCash(fund)
				.add(getAmountOfGift(fund))
				.add(getTotalAmountOfCoupon(fund))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getPaidFreeCard()));
	}

	//获取订单中支付的红包金额
	public static BigDecimal getAmountOfGift(Fund fund) {
		return fund.getPaidGif().add(fund.getPostGif())
				.subtract(fund.getRefundGif());
	}

	public static BigDecimal getTotalAmountOfCoupon(Fund fund) {
		return DataUtil.BigDecimall
				.GetValueOrDefault(fund.getYmtCoupon())
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getSellerCoupon()))
				.subtract(
						DataUtil.BigDecimall.GetValueOrDefault(fund
								.getRefundCoupon()));
	}

	//获取退款总金额
	public static BigDecimal getRefundedAmount(Fund fund) {
		if (fund == null) {
			return BigDecimal.ZERO;
		}

		return DataUtil.BigDecimall
				.GetValueOrDefault(fund.getRefundCash())
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund.getRefundGif()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getRefundCoupon()))
				.add(DataUtil.BigDecimall.GetValueOrDefault(fund
						.getRefundDiscount3P()));
	}

	public static Integer getSalesRefundStatus(List<Refund> refunds) {
		if (CollectionUtils.isEmpty(refunds)) {
			return null;
		} else {
			List<Integer> refundStatus = refunds.stream()
					.map(e -> e.getRefundStatus()).collect(Collectors.toList());
			if (refundStatus.contains(SalesRefundStatusEnum.Processing
					.getValue())) {
				return SalesRefundStatusEnum.Processing.getValue();
			} else if (refundStatus.contains(SalesRefundStatusEnum.Success
					.getValue())) {
				return SalesRefundStatusEnum.Success.getValue();
			} else {
				return SalesRefundStatusEnum.Fail.getValue();
			}
		}
	}

	public static SubProductInfo buildSubProduct(SubProduct subProduct) {
		SubProductInfo product = new SubProductInfo();
		product.setCatalogId(subProduct.getCatalogId());
		product.setDiscount(subProduct.getDiscount());
		product.setFirstCatId(subProduct.getFirstCatId());
		product.setFreight(subProduct.getFreight());
		product.setNum(subProduct.getNum());
		product.setOriginalPrice(subProduct.getOriginalPrice());
		product.setPrice(subProduct.getPrice());
		product.setPromotion(subProduct.getPromotion());
		product.setSecondCatId(subProduct.getSecondCatId());
		product.setSellerCoupon(subProduct.getSellerCoupon());
		product.setSku(subProduct.getSku());
		product.setSubCatalogId(subProduct.getSubCatalogId());
		product.setSubTitle(subProduct.getSubTitle());
		product.setThirdCatId(subProduct.getThirdCatId());
		product.setThirdPartyDiscount(subProduct.getThirdPartyDiscount());
		product.setYmtCoupon(subProduct.getYmtCoupon());
		return product;
	}

	public static List<SubProductInfo> buildSubProducts(Order order,
			String catalogId) {
		if (!CollectionUtils.isEmpty(order.getMapProducts())) {
			List<SubProduct> list = order.getMapProducts().get(catalogId);
			if (!CollectionUtils.isEmpty(list)) {
				List<SubProductInfo> rtnList = new ArrayList<SubProductInfo>();
				for (SubProduct subProduct : list) {
					rtnList.add(buildSubProduct(subProduct));
				}
				return rtnList;
			}
		}
		return null;
	}
}
